﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LWrap.IndexSource;
namespace LWrap.Index
{
    public static class IndexingMonitor
    {
        static IndexWriter _indexWriter;
        public static string IndexFilePath { get; set; }
        public static bool IsCreateNewIndex { get; set; }


        public static void InitiateIndex(IndexDataSource indexSource,string indexFilePath, bool isCreateNewIndex)
        {
            if (_indexWriter == null)
            {
                _indexWriter = new IndexWriter(indexFilePath, isCreateNewIndex);
            }
            if (_indexWriter.IsIndexModificationInProgress == false)
                _indexWriter.Execute(indexSource);
            else
                throw new System.IO.IOException("Cannot open the Index File. Index files are locked by another process. Try after some time.");
        }

        public static void InitiateIndex(IndexDataSource indexSource)
        {
            InitiateIndex(indexSource, IndexFilePath, IsCreateNewIndex);
        }

        public static void DeleteDocuments(string booleanCondition,string indexFilePath)
        {
            if (_indexWriter == null)
            {
                _indexWriter = new IndexWriter(indexFilePath, false);
            }
            if(_indexWriter.IsIndexModificationInProgress==false)
                _indexWriter.DeleteDocument(booleanCondition);
            else
                throw new System.IO.IOException("Cannot open the Index File. Index files are locked by another process. Try after some time.");
        }
    }
}
